package com.ruoyi.eam.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.eam.complex.UpdateEamChg;
import com.ruoyi.eam.constants.EamConstants;
import com.ruoyi.eam.doman.*;
import com.ruoyi.eam.service.*;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 资产变更Controller
 *
 * @author ruoyi
 * @date 2024-07-01
 */
@RestController
@RequestMapping("/eamChange")
public class EamChgController extends BaseController
{
    @Autowired
    private IEamChgHService eamChgHService;
    @Autowired
    private IEamTypeService eamTypeService;
    @Autowired
    private IEamLocationService locationService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IEamChgDService eamChgDService;
    @Autowired
    private RedissonClient redissonClient;

    /**
     * 查询资产变更列表
     */
    //@RequiresPermissions("eam:eamChange:list")
    @GetMapping("/list")
    public TableDataInfo list(EamChgH eamChgH)
    {
        startPage();
        List<EamChgH> list = eamChgHService.selectEamChgHList(eamChgH);
        return getDataTable(list);
    }


    /**
     * 查询资产类别,存放位置,部门列表
     */
    //@RequiresPermissions("system:eamChange:typeList")
    @GetMapping("/typeList")
    public Map<String,Object> typeList(EamChgH eamChgH)
    {
        Map<String,Object> rm = new HashMap<>();
        startPage();
        List<EamType> typeList = eamTypeService.selectEamTypeList(null);
        List<EamLocation> locations = locationService.selectEamLocationList(null);
        List<Dept> depts = deptService.selectDeptList(null);
        rm.put("typeList",typeList);
        rm.put("locationList",locations);
        rm.put("deptList",depts);
        return rm;
    }

    /**
     * 查询资产类别,存放位置,部门列表
     */
    //@RequiresPermissions("eam:eamChange:userListByDeptId")
    @GetMapping("/userListByDeptId")
    public Map<String,Object> userListByDeptId(User user)
    {
        Map<String,Object> rm = new HashMap<>();
        List<User> userList = userService.selectUserList(user);
        rm.put("userList",userList);
        return rm;
    }

    /**
     * 导出资产变更列表
     */
    //@RequiresPermissions("eam:eamChange:export")
    @Log(title = "资产变更", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamChgH eamChgH)
    {
        List<EamChgH> list = eamChgHService.selectEamChgHList(eamChgH);
        ExcelUtil<EamChgH> util = new ExcelUtil<EamChgH>(EamChgH.class);
        util.exportExcel(response, list, "资产变更数据");
    }

    /**
     * 获取资产变更详细信息
     */
    //@RequiresPermissions("eam:eamChange:query")
    @GetMapping(value = "/{alterationId}")
    public AjaxResult getInfo(@PathVariable("alterationId") Long alterationId)
    {
        return success(eamChgHService.selectEamChgHByAlterationId(alterationId));
    }

    /**
     * 新增资产变更
     */
    //@RequiresPermissions("eam:eamChange:add")
    @Log(title = "资产变更", businessType = BusinessType.INSERT)
    @PostMapping("/addH")
    public AjaxResult addH(@RequestBody EamChgH eamChgH)
    {
        System.out.println("资产变更:"+eamChgH);
        //锁
        RLock lock = redissonClient.getLock("myinsertChgCode");
        try{
            if (!lock.tryLock()) {
                return AjaxResult.error("操作过于频繁");
            }else{
                return toAjax(eamChgHService.insertEamChgH(eamChgH));
            }
        }finally {
            //释放
            lock.unlock();

        }
    }


    /**
     * 新增资产变更从表
     */
    //@RequiresPermissions("eam:eamChange:addD")
    @Log(title = "资产借用", businessType = BusinessType.INSERT)
    @PostMapping("/addD")
    public AjaxResult addD(@RequestBody List<EamAsset> eamAssets)
    {
        //锁
        RLock lock = redissonClient.getLock("myinsertChgDCode");
        try{
            if (!lock.tryLock()) {
                return AjaxResult.error("操作过于频繁");
            }else{
                return toAjax(eamChgDService.insertEamChgD(eamAssets));
            }
        }finally {
            //释放
            lock.unlock();

        }
    }

    /**
     * 修改资产变更
     */
    //@RequiresPermissions("eam:h:edit")
    @Log(title = "资产变更", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody EamChgH eamChgH)
    {
        return toAjax(eamChgHService.updateEamChgH(eamChgH));
    }

    /**
     * 撤销变更
     */
    //@RequiresPermissions("eam:h:retrunChgStatus")
    @Log(title = "资产变更", businessType = BusinessType.UPDATE)
    @PutMapping("/retrunChgStatus")
    public AjaxResult retrunChgStatus(@RequestBody EamChgH eamChgH)
    {
        return toAjax(eamChgHService.returnChgReceiptsStatus(eamChgH));
    }

    /**
     * 删除资产变更
     */
    //@RequiresPermissions("eam:h:remove")
    @Log(title = "资产变更", businessType = BusinessType.DELETE)
	@DeleteMapping("delChgH/{alterationId}")
    public AjaxResult remove(@PathVariable Long alterationId)
    {
        return toAjax(eamChgHService.deleteEamChgHByAlterationId(alterationId));
    }

    /**
     * 修改资产变更
     */
    //@RequiresPermissions("eam:h:updateChgD")
    @Log(title = "修改:删除变更从表", businessType = BusinessType.DELETE)
    @PostMapping("/updateChgD")
    public AjaxResult delChgD(@RequestBody UpdateEamChg updateEamChg)
    {
        //先做删除从表操作
        eamChgDService.deleteEamChgDByAlterationId(updateEamChg.getEamChgH().getAlterationId());
        //在做添加从表操作
        eamChgDService.insertChgD(updateEamChg.getEamAssets(),updateEamChg.getEamChgH().getAlterationId());
        //修改主
        return toAjax(eamChgHService.updateEamChgH(updateEamChg.getEamChgH()));
    }

}
